/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelLimparPalete.java
 *
 * Created on 23/09/2011, 06:02:30
 */
package modules.enderecamento.gui.jpanel;

import java.util.List;
import javax.swing.table.DefaultTableModel;
import modules.enderecamento.facade.WMSFacade;
import modules.enderecamento.gui.custom.LimitedIntegerJTextField;
import modules.enderecamento.models.dto.Enderecamento;
import modules.enderecamento.models.dto.Palete;
import modules.enderecamento.models.dto.Produto;
import modules.enderecamento.util.BoxPanel;

/**
 *
 * @author Clezio
 */
public class PanelLimparPalete extends javax.swing.JPanel {

    private WMSFacade wmsFacade;
    private Integer paleteConsultadoGlobal;

    /** Creates new form PanelLimparPalete */
    public PanelLimparPalete() {
        initComponents();
        wmsFacade = new WMSFacade();
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(93);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(303);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(26);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbCubagem = new modules.enderecamento.gui.custom.WMSLabel();
        btnLimpar = new modules.enderecamento.gui.custom.WMSJButton();
        tfPalete = new LimitedIntegerJTextField(10);
        btnConsultar = new modules.enderecamento.gui.custom.WMSJButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new modules.enderecamento.gui.custom.WMSJTable();

        setBackground(new java.awt.Color(238, 238, 238));
        setPreferredSize(new java.awt.Dimension(800, 586));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Remoção de Produtos do Palete");

        lbCubagem.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbCubagem.setText("Palete:");

        btnLimpar.setMnemonic('L');
        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        tfPalete.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfPaleteKeyPressed(evt);
            }
        });

        btnConsultar.setMnemonic('C');
        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });
        btnConsultar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnConsultarKeyPressed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Código", "Descrição", "Qtde"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 772, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(lbCubagem, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(tfPalete, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(132, 132, 132)
                .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(117, 117, 117)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbCubagem, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfPalete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed

        removerProdutosPalete();
}//GEN-LAST:event_btnLimparActionPerformed

    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        consultarProdutosPalete();
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnConsultarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnConsultarKeyPressed
        
    }//GEN-LAST:event_btnConsultarKeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        
    }//GEN-LAST:event_jTable1MouseClicked

    private void tfPaleteKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPaleteKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            if (tfPalete.getText().length() > 0) {
                consultarProdutosPalete();
            }
        } else if (evt.getKeyCode() == evt.VK_ESCAPE) {
            tfPalete.setText("");
        }
    }//GEN-LAST:event_tfPaleteKeyPressed

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        tfPalete.requestFocus();
    }//GEN-LAST:event_formAncestorAdded

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private modules.enderecamento.gui.custom.WMSJButton btnConsultar;
    private modules.enderecamento.gui.custom.WMSJButton btnLimpar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private modules.enderecamento.gui.custom.WMSJTable jTable1;
    private modules.enderecamento.gui.custom.WMSLabel lbCubagem;
    private modules.enderecamento.gui.custom.WMSJTextField tfPalete;
    // End of variables declaration//GEN-END:variables

    private void removerProdutosPalete() {
        if(paleteConsultadoGlobal != null){
            if(BoxPanel.showConfirmDialog(null,"Deseja realmente remover os produtos endereçados nesse Palete?") == 0){
                if(new WMSFacade().removeProdutosPalete(paleteConsultadoGlobal)){
                    DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                    modelo.setNumRows(0);
                    BoxPanel.showMessageDialog(null, "Operação realizada com sucesso.");
                }
            }
        }else{
            BoxPanel.showMessageDialog(null, "Efetue a consulta de um Palete");
        }
    }

    private void consultarProdutosPalete() {
        if (tfPalete.getText().length() > 0) {
            paleteConsultadoGlobal = Integer.parseInt(tfPalete.getText());
            List<Enderecamento> endecamentos = wmsFacade.obterEnderecamento("from Enderecamento where palete_id = " + tfPalete.getText());
            if(endecamentos != null){
                if(endecamentos.isEmpty()){
                    Palete palete = wmsFacade.obterPaleteById(Integer.parseInt(tfPalete.getText()));
                    if(palete == null){
                        BoxPanel.showMessageDialog(null, "Palete não encontrado.");
                    }else{
                        BoxPanel.showMessageDialog(null, "O Palete se encontra vazio.");
                    }
                    paleteConsultadoGlobal = null;
                }
                DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
                modelo.setNumRows(0);

                for (int i = 0; i < endecamentos.size(); i++) {
                    Produto produto = endecamentos.get(i).getProduto();
                    Integer qtde = endecamentos.get(i).getQuantidade();

                    modelo.addRow(new Object[]{
                                produto.getCodigo(), produto.getDescricao(), qtde
                            });
                }
            }
        }else{
            BoxPanel.showMessageDialog(null, "Informe o Palete");
        }
    }
}
